//
//  UrbanHomeTableViewCell.m
//  UrbanManagement
//
//  Created by Can on 18/4/10.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanHomeTableViewCell.h"

@interface UrbanHomeTableViewCell ()
{
    CGFloat h;
}

@end

@implementation UrbanHomeTableViewCell

// 1. 初始化子视图
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        
    }
    
    return self;
}

- (void)loadData:(NSDictionary *)data number:(NSInteger)num{
    
    for (UIView *subView in self.contentView.subviews)
    {
        [subView removeFromSuperview];
    }

    switch ([[data objectForKey:@"orderState"] intValue]) {

        case 1:
        {
            CGFloat width = [TomamaUI getStringWeithtWith:@"罚 ¥ 100" andHeigth:20*screenW_6 andFont:[UIFont systemFontOfSize:20*screenW_6]];
            //罚款
            UILabel *_fineLable = [TomamaUI createLabel:CGRectMake(17*screenW_6, 22*screenW_6, width, 20*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:20*screenW_6] textColor:Color(255, 124, 60) text:@"罚 ¥ 100"];
            [self.contentView addSubview:_fineLable];
            
            CGFloat widthState = [TomamaUI getStringWeithtWith:@"待办理" andHeigth:20*screenW_6 andFont:[UIFont systemFontOfSize:20*screenW_6]];
            //罚款
            UILabel *stateLable = [TomamaUI createLabel:CGRectMake(CGRectGetMaxX(_fineLable.frame) + 10*screenW_6, 22*screenW_6, widthState, 20*screenW_6) textAligment:1 font:[UIFont systemFontOfSize:15*screenW_6] textColor:Color(255, 124, 60) text:@"待办理"];
            [self.contentView addSubview:stateLable];
            stateLable.layer.borderColor = Color(255, 124, 60).CGColor;
            stateLable.layer.borderWidth = 1.0;
        }
            break;
            
        case 2:
        {
            CGFloat width = [TomamaUI getStringWeithtWith:@"罚 ¥ 100" andHeigth:20*screenW_6 andFont:[UIFont systemFontOfSize:20*screenW_6]];
            //罚款
            UILabel *_fineLable = [TomamaUI createLabel:CGRectMake(17*screenW_6, 22*screenW_6, width, 20*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:20*screenW_6] textColor:Color(255, 124, 60) text:@"罚 ¥ 100"];
            [self.contentView addSubview:_fineLable];
            
            CGFloat widthState = [TomamaUI getStringWeithtWith:@"已办理" andHeigth:20*screenW_6 andFont:[UIFont systemFontOfSize:20*screenW_6]];
            //罚款
            UILabel *stateLable = [TomamaUI createLabel:CGRectMake(CGRectGetMaxX(_fineLable.frame) + 10*screenW_6, 22*screenW_6, widthState, 20*screenW_6) textAligment:1 font:[UIFont systemFontOfSize:15*screenW_6] textColor:Color(255, 124, 60) text:@"已办理"];
            [self.contentView addSubview:stateLable];
            stateLable.layer.borderColor = Color(255, 124, 60).CGColor;
            stateLable.layer.borderWidth = 1.0;
        }
            break;
            
        case 3:
        {
            CGFloat width = [TomamaUI getStringWeithtWith:@"警告处理" andHeigth:20*screenW_6 andFont:[UIFont systemFontOfSize:20*screenW_6]];
            //罚款
            UILabel *_fineLable = [TomamaUI createLabel:CGRectMake(17*screenW_6, 22*screenW_6, width, 20*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:20*screenW_6] textColor:Color(255, 124, 60) text:@"警告处理"];
            [self.contentView addSubview:_fineLable];
        }
            break;
            
        case 4:
        {
            CGFloat width = [TomamaUI getStringWeithtWith:@"罚 ¥ 100" andHeigth:20*screenW_6 andFont:[UIFont systemFontOfSize:20*screenW_6]];
            //罚款
            UILabel *_fineLable = [TomamaUI createLabel:CGRectMake(17*screenW_6, 22*screenW_6, width, 20*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:20*screenW_6] textColor:Color(255, 124, 60) text:@"罚 ¥ 100"];
            [self.contentView addSubview:_fineLable];
            
            CGFloat widthState = [TomamaUI getStringWeithtWith:@"已移送交警" andHeigth:20*screenW_6 andFont:[UIFont systemFontOfSize:20*screenW_6]];
            //罚款
            UILabel *stateLable = [TomamaUI createLabel:CGRectMake(CGRectGetMaxX(_fineLable.frame) + 10*screenW_6, 22*screenW_6, widthState, 20*screenW_6) textAligment:1 font:[UIFont systemFontOfSize:15*screenW_6] textColor:Color(255, 124, 60) text:@"已移送交警"];
            [self.contentView addSubview:stateLable];
            stateLable.layer.borderColor = Color(255, 124, 60).CGColor;
            stateLable.layer.borderWidth = 1.0;
        }
            break;
            
        default:
            break;
    }
    
    //时间
    UILabel *_timeLable = [TomamaUI createLabel:CGRectMake(0 , 29*screenW_6, SCREENW - 27*screenW_6, 13*screenW_6) textAligment:2 font:[UIFont systemFontOfSize:13*screenW_6] textColor:Color(51, 51, 51) text:[data objectForKey:@"carParkTime"]];
    [self.contentView addSubview:_timeLable];
    
    //地址
    UILabel *_whereLable = [TomamaUI createLabel:CGRectMake(17*screenW_6, 42*screenW_6 + 15*screenW_6, SCREENW - 54*screenW_6, 13*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:13*screenW_6] textColor:Color(153, 153, 153) text:[NSString stringWithFormat:@"地址:%@%@",[data objectForKey:@"carAddress"],[data objectForKey:@"addressDetails"]]];
    _whereLable.frame = CGRectMake(17*screenW_6, CGRectGetMaxY(_whereLable.frame) + 15*screenW_6, SCREENW - 54*screenW_6, [self lableHeight:_whereLable]);
    [self.contentView addSubview:_whereLable];
    
    //行为
    UILabel *_activityLable = [TomamaUI createLabel:CGRectMake(17*screenW_6, CGRectGetMaxY(_whereLable.frame) + 15*screenW_6, SCREENW - 54*screenW_6, 13*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:13*screenW_6] textColor:Color(153, 153, 153) text:@"行为:机动车违反规定停放，临时停车且驾驶人不在现场或者虽然在现场但驾驶人拒绝立即驶离，妨碍其他车辆行人通行"];
    _activityLable.frame = CGRectMake(17*screenW_6, CGRectGetMaxY(_whereLable.frame) + 15*screenW_6, SCREENW - 54*screenW_6, [self lableHeight:_activityLable]);
    [self.contentView addSubview:_activityLable];

    //采集机关
    UILabel *_organLable = [TomamaUI createLabel:CGRectMake(17*screenW_6, CGRectGetMaxY(_activityLable.frame) + 15*screenW_6, SCREENW - 54*screenW_6, 13*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:13*screenW_6] textColor:Color(153, 153, 153) text:@"采集机关:黄冈一城管大队"];
    _organLable.frame = CGRectMake(17*screenW_6, CGRectGetMaxY(_activityLable.frame) + 15*screenW_6, SCREENW - 54*screenW_6, [self lableHeight:_organLable]);
    [self.contentView addSubview:_organLable];
    
    //地图位子
    UIButton *_whereBtn = [TomamaUI createButton:CGRectMake(290/2*screenW_6, CGRectGetMaxY(_organLable.frame) + 15*screenW_6, 95*screenW_6, 35*screenW_6) targ:self sel:@selector(where:) titleColor:Color(250, 119, 133) title:@"违法位置" image:@"positon" backGroundImage:@"red_button"];
    _whereBtn.tag = num;
    [self.contentView addSubview:_whereBtn];
    
    //现场照片
    UIButton *_photoBtn = [TomamaUI createButton:CGRectMake(CGRectGetMaxX(_whereBtn.frame) + 10*screenW_6, CGRectGetMaxY(_organLable.frame) + 15*screenW_6, 95*screenW_6, 35*screenW_6) targ:self sel:@selector(photo:) titleColor:Color(102, 102, 102) title:@"现场照片" image:@"pic" backGroundImage:@"hui_button"];
    _photoBtn.tag = num;
    [self.contentView addSubview:_photoBtn];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(15*screenW_6, CGRectGetMaxY(_organLable.frame) + 15*screenW_6, 95*screenW_6, 35*screenW_6);
    [button setTitle:@"打印罚单" forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"icon06"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13*screenW_6];
    [button addTarget:self action:@selector(printBtn:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = num;
    [self.contentView addSubview:button];
    
    h = CGRectGetMaxY(_whereBtn.frame) + 15*screenW_6;
}

- (CGFloat)lableHeight:(UILabel *)lable{
    lable.numberOfLines = 0;//根据最大行数需求来设置
    lable.lineBreakMode = NSLineBreakByTruncatingTail;
    CGSize maximumLabelSize = CGSizeMake(SCREENW - 54*screenW_6, 9999);//labelsize的最大值
    //关键语句
    CGSize expectSize = [lable sizeThatFits:maximumLabelSize];
    return expectSize.height;
}

- (CGFloat)btnHeight{
    return h;
}

- (void)where:(UIButton *)btn
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeWhere:)]) {
        [self.delegate homeWhere:btn.tag];
    }
}

- (void)photo:(UIButton *)btn
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(homePhone:)]) {
        [self.delegate homePhone:btn.tag];
    }
}

- (void)printBtn:(UIButton *)btn
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(printer:)]) {
        [self.delegate printer:btn.tag];
    }
}

+(CGFloat)cellHeight:(NSDictionary *)data number:(NSInteger)num{
    UrbanHomeTableViewCell *cell = [[UrbanHomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeCell"];
    [cell loadData:data number:num];
    return [cell btnHeight];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setFrame:(CGRect)frame{

    [super setFrame:frame];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = path.CGPath;
    self.layer.mask = maskLayer;
}

- (NSString *)getTime:(NSString *)time
{
    NSTimeInterval interval    =[time doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString       = [formatter stringFromDate: date];
    return dateString;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
